package net.zjitc.service.impl;


import net.zjitc.common.Msg;

import net.zjitc.common.ServiceResponse;
import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author admin
 * @Date 2019/4/30 11:06
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void register(User user) {
        userMapper.register(user);
    }

    @Override
    public User findByName(String username) {
        return userMapper.findByName(username);
    }

    @Override
    public ServiceResponse update(User user, HttpServletRequest request,HttpSession session) {
        User user1 = (User) session.getAttribute("user");
        user.setId(user1.getId());
        int i = userMapper.update(user);
        if (i > 0){
            return ServiceResponse.createBySuccessAndMessage("修改成功");
        }
        return ServiceResponse.createByErrorMessage("修改失败");
    }

    @Override
    public User findById(int username) {
        return userMapper.findById(username);
    }

    @Override
    public Msg checkEmail(User user) {
        return getMsg(user);
    }

    private Msg getMsg(User user) {
        int resoutCount = userMapper.checkEmail(user.getEmail());
        Msg msg = new Msg();
        if (resoutCount == 0){
            msg.setFlag(true);
        }else {
            msg.setFlag(false);
        }
        return msg;
    }

    @Override
    public Msg checkUsername(User user) {
        return getMsg(user);
    }
}